package com.xiege.springboot.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xiege.springboot.entity.Way;
import com.xiege.springboot.mapper.WayMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
@Component
public class ThreadService {
    @Resource
    private WayMapper wayMapper;

    @Async("taskExecutor")
    public void updateWayViewCount(WayMapper wayMapper, Way way){
        int viewCounts = way.getViewCount();
        Way wayUpdate = new Way();
        wayUpdate.setViewCount(viewCounts +1);
        LambdaUpdateWrapper<Way> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Way::getId,way.getId());
        //设置一个 为了在多线程的环境下 线程安全
        updateWrapper.eq(Way::getViewCount,viewCounts);
        // update Way set view_count=100 where view_count=99 and id=11
        wayMapper.update(wayUpdate,updateWrapper);
    }
}
